﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Web.MVP;
using Medianamik.UI.Web.MVP.View;

namespace Medianamik.UI.Web.TypeList
{
    public partial class NodeTypeList : ModuleViewWithGrid<ITypeList>, ITypeList
    {
        public event EventHandler<IdEventArgs> NodeTypeChanged;

        protected void Page_Init(object sender, EventArgs e)
        {
            NodeTypesDropDownList.SelectedIndexChanged += OnNodeTypeChanged;
            FilterButton.Click += OnFilterButton;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        void OnFilterButton(object sender, EventArgs e)
        {
            OnFilter();
        }

        protected virtual void OnNodeTypeChanged(IdEventArgs e)
        {
            var handler = NodeTypeChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public override IMedianamikGrid Grid
        {
            get { return (IMedianamikGrid)TypeListGrid; }
        }

        public override HyperLink AddButton
        {
            get { return AddItemButton; }
        }

        public override string Keywords
        {
            get { return Name.Text; }
            set { Name.Text = value; }
        }

        public override string ListingPageUrl
        {
            get { return Pages.TypeList; }
        }

        public override string EditPageUrl
        {
            get { return Pages.Edit; }
        }

        public override string RootListingPageUrl
        {
            get { return Pages.TypeList; }
        }

        public override bool IsMultiLingual
        {
            get { return true; }
        }

        public IList<NodeType> NodeTypes
        {
            set { BindNodeTypesDropDownList(value); }
        }

        private Guid? _baseTypeId;
        public override Guid BaseTypeId
        {
            get { return (_baseTypeId ?? (_baseTypeId = GetSelectedNodeTypeFromDropDownList())).Value; }
        }

        private Guid GetSelectedNodeTypeFromDropDownList()
        {
            if (SelectedNodeTypeDropDownList != "0")
            {
                Guid selectedNodeType;
                if (StringHelper.IsGuid(SelectedNodeTypeDropDownList, out selectedNodeType))
                {
                    return selectedNodeType;
                }
            }
            return Guid.Empty;
        }

        private string _typeSelectorUrl;
        public override string TypeSelectorUrl
        {
            get { return _typeSelectorUrl ?? (_typeSelectorUrl = GetTypeSelectorUrl()); }
        }
        private string GetTypeSelectorUrl()
        {
            var typeSelectorUrl = Pages.TypeSelector;/*.AddParam(DefaultParamNames.ParentNodeID, ModuleNodeId);*/
            if (BaseTypeId != Guid.Empty)
                typeSelectorUrl = typeSelectorUrl.AddParam(DefaultParamNames.TypeID, BaseTypeId);
            return typeSelectorUrl;
        }

        public override bool AllowSorting
        {
            get { return true; }
        }

        public override bool AllowPaging
        {
            get { return false; }
        }

        private void BindNodeTypesDropDownList(IList<NodeType> nodeTypes)
        {
            NodeTypesDropDownList.DataSource = nodeTypes;
            NodeTypesDropDownList.DataValueField = "ID";
            NodeTypesDropDownList.DataTextField = "LocalizedName";
            NodeTypesDropDownList.DataBind();

            if (CurrentTypeIdQueryString != Guid.Empty)
            {
                var listItem = NodeTypesDropDownList.Items.FindByValue(CurrentTypeIdQueryString.ToString());
                if (listItem != null)
                {
                    NodeTypesDropDownList.SelectedValue = listItem.Value;
                }
                else
                    AddAndSelectEmptyListItem();
            }
            else
                AddAndSelectEmptyListItem();

            SetPlaceHolders();
        }

        private void AddAndSelectEmptyListItem()
        {
            NodeTypesDropDownList.Items.Insert(0, new ListItem("", "0"));
            NodeTypesDropDownList.SelectedIndex = 0;
        }

        private void SetPlaceHolders()
        {
            ValueSelectedPlaceHolder.Visible = SelectedNodeTypeDropDownList != "0";
        }

        protected string BuildReturnUrl()
        {
            var url = ReturnUrl;
            if (AllowPaging)
            {
                if (Grid.PageSize != 20)
                    url = url.AddParam(ParamNames.PageSize, Grid.PageSize);

                if (Grid.PageIndex != 0)
                    url = url.AddParam(ParamNames.PageIndex, Grid.PageIndex);
            } 
            return string.Concat("&", DefaultParamNames.ReturnURL, "=") + 
                (SelectedNodeTypeDropDownList != "0" ? 
                    url.AddParam(DefaultParamNames.TypeID, SelectedNodeTypeDropDownList) : url).UrlEncodeBase64();
        }

        private string SelectedNodeTypeDropDownList
        {
            get { return NodeTypesDropDownList.SelectedValue; }
        }

        private Guid? _currentTypeIdQueryString;
        private Guid CurrentTypeIdQueryString
        {
            get
            {
                return (_currentTypeIdQueryString ?? 
                        (_currentTypeIdQueryString = GetCurrentTypeIdFromQueryString()))
                        .GetValueOrDefault(Guid.Empty);
            }
        }
        private Guid? GetCurrentTypeIdFromQueryString()
        {
            return GetQueryStringValue<Guid?>(DefaultParamNames.TypeID);
        }

        protected void OnNodeTypeChanged(object sender, EventArgs e)
        {
            OnNodeTypeChanged(new IdEventArgs(SelectedNodeTypeDropDownList));
            SetPlaceHolders();
        }
    }

    public interface ITypeList : IModuleViewWithGrid
    {
        IList<NodeType> NodeTypes { set; }
        event EventHandler<IdEventArgs> NodeTypeChanged;
    }
}
